import db from "@src/dataBase/db";
import Sequelize from "sequelize";
import File from "@src/models/file";
import Dict from "@src/models/dict";

const Literature = db.define("literature",
  {
    id: {type: Sequelize.STRING, primaryKey: true},
    name: {type: Sequelize.STRING(20)},
    author: {type: Sequelize.STRING(20)},
    press: {type: Sequelize.STRING(20)},
    introduce: {type: Sequelize.TEXT},
    file_id: {type: Sequelize.STRING},
    cover_id: {type: Sequelize.STRING},
    code: {type: Sequelize.INTEGER},
    category_id: {type: Sequelize.STRING},
    region_id: {type: Sequelize.STRING},
    recommend: {type: Sequelize.INTEGER},
    grounding: {type: Sequelize.INTEGER},
    visit_num: {type: Sequelize.INTEGER},
    created_at: {type: Sequelize.BIGINT},
    updated_at: {type: Sequelize.BIGINT},
  }, {
    timestamps: false,
    underscored: true,
    tableName: "literature",
  },
);

// 一对一，文献关联文件表
Literature.belongsTo(File, {
  // 本表的字段
  foreignKey: {
    field: "file_id",
  },
  as: "file",
  // 目标字段
  targetKey: "id",
});

// 一对一，文献关联文件表
Literature.belongsTo(File, {
  // 本表的字段
  foreignKey: {
    field: 'cover_id',
  },
  as: "cover",
  // 目标字段
  targetKey: "id",
});

// 一对一，文献关联字典表
Literature.belongsTo(Dict, {
  //本表的字段
  foreignKey: "category_id",
  as: "dict",
  // 目标字段
  targetKey: "id",
});

export default Literature;